home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / filutil / bison110.zip / DERIVES.C < prev    next >
C/C++ Source or Header  |  1990-06-26  |  2KB  |  118 lines

  1. /* Match rules with nonterminals for bison,
  2.    Copyright (C) 1984, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* set_derives finds, for each variable (nonterminal), which rules can derive it.
  22.    It sets up the value of derives so that
  23.    derives[i - ntokens] points to a vector of rule numbers, terminated with a zero.  */
  24.  
  25. #include <stdio.h>
  26. #include "system.h"
  27. #include "new.h"
  28. #include "types.h"
  29. #include "gram.h"
  30.  
  31.  
  32. short **derives;
  33.  
  34. void
  35. set_derives()
  36. {
  37.   register int i;
  38.   register int lhs;
  39.   register shorts *p;
  40.   register short *q;
  41.   register shorts **dset;
  42.   register shorts *delts;
  43.  
  44.   dset = NEW2(nvars, shorts *) - ntokens;
  45.   delts = NEW2(nrules + 1, shorts);
  46.  
  47.   p = delts;
  48.   for (i = nrules; i > 0; i--)
  49.     {
  50.       lhs = rlhs[i];
  51.       if (lhs >= 0)
  52.     {
  53.       p->next = dset[lhs];
  54.       p->value = i;
  55.       dset[lhs] = p;
  56.       p++;
  57.     }
  58.     }
  59.  
  60.   derives = NEW2(nvars, short *) - ntokens;
  61.   q = NEW2(nvars + nrules, short);
  62.  
  63.   for (i = ntokens; i < nsyms; i++)
  64.     {
  65.       derives[i] = q;
  66.       p = dset[i];
  67.       while (p)
  68.     {
  69.       *q++ = p->value;
  70.       p = p->next;
  71.     }
  72.       *q++ = -1;
  73.     }
  74.  
  75. #ifdef    DEBUG
  76.   print_derives();
  77. #endif
  78.  
  79.   FREE(dset + ntokens);
  80.   FREE(delts);
  81. }
  82.  
  83. void
  84. free_derives()
  85. {
  86.   FREE(derives[ntokens]);
  87.   FREE(derives + ntokens);
  88. }
  89.  
  90.  
  91.  
  92. #ifdef    DEBUG
  93.  
  94. print_derives()
  95. {
  96.   register int i;
  97.   register short *sp;
  98.  
  99.   extern char **tags;
  100.  
  101.   printf("\n\n\nDERIVES\n\n");
  102.  
  103.   for (i = ntokens; i < nsyms; i++)
  104.     {
  105.       printf("%s derives", tags[i]);
  106.       for (sp = derives[i]; *sp > 0; sp++)
  107.     {
  108.       printf("  %d", *sp);
  109.     }
  110.       putchar('\n');
  111.     }
  112.  
  113.   putchar('\n');
  114. }
  115.  
  116. #endif
  117.  
  118.